package com.future.hedongjun.mine.order;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.GlideUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.common.PriceHelper;
import com.future.hedongjun.mine.bean.ClassOrderItem;
import com.future.hedongjun.mine.bean.response.ClassOrderResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ClassOrderDetailActivity extends BaseActivity {

    @BindView(R.id.item_iv_1)
    ImageView itemIv1;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_price_commodity)
    TextView tvPriceCommodity;
    @BindView(R.id.layout_price_commodity)
    FrameLayout layoutPriceCommodity;
    @BindView(R.id.tv_coupon_price)
    TextView tvCouponPrice;
    @BindView(R.id.tv_price_yf)
    TextView tvPriceYf;
    @BindView(R.id.layout_price_yf)
    FrameLayout layoutPriceYf;
    @BindView(R.id.tv_tip_order_num)
    TextView tvTipOrderNum;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_tip_order_time)
    TextView tvTipOrderTime;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_tip_order_type)
    TextView tvTipOrderType;
    @BindView(R.id.tv_order_type)
    TextView tvOrderType;
    @BindView(R.id.tv_copy_order_num)
    TextView tvCopyOrderNum;
    @BindView(R.id.btn_cancel_order)
    TextView btnCancelOrder;
    @BindView(R.id.bottom_btn)
    Button bottomBtn;
    private String id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_order_detail);
        ButterKnife.bind(this);
        setTitle("课程订单详情");
//        ClassOrderItem data = (ClassOrderItem) getIntent().getSerializableExtra("data");
        getData();
//        bindView(data);
    }

    private void getData() {
        new HttpRequest<ClassOrderResponse>()
                .with(this)
                .setApiCode("_myclassindentdetail_001")
                .addParam("userToken",MyApp.getInstance().getUserToken())
                .addParam("id",id)
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        bindView(((ClassOrderResponse) response).data);
                    }
                }).start(new ClassOrderResponse());
    }

    private void bindView(ClassOrderItem orderItem) {
        if (orderItem == null) {
            return;
        }
        GlideUtils.loadImage(this, orderItem.getLogo_img(), itemIv1);
        tvName.setText(orderItem.getName());
        tvPrice.setText(PriceHelper.priceFormat(orderItem.getPrice()));
        tvPriceCommodity.setText(PriceHelper.priceFormat(orderItem.getPrice()));
        tvCouponPrice.setText(PriceHelper.priceFormat(orderItem.getDiscount_price()));
        tvPriceYf.setText(PriceHelper.priceFormat(orderItem.getAmount()));
        tvOrderNum.setText(orderItem.getOrder_num());
        tvOrderTime.setText(orderItem.getCreate_time());
        switch (orderItem.getStatus()) {
            case 1:
                tvOrderType.setText("待支付");
                break;
            case 2:
                tvOrderType.setText("已取消");
                btnCancelOrder.setVisibility(View.GONE);
                bottomBtn.setVisibility(View.GONE);
                break;
            case 3:
                tvOrderType.setText("已完成");
                btnCancelOrder.setVisibility(View.GONE);
                bottomBtn.setVisibility(View.GONE);
                break;
        }

    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);

        id = extras.getString("id");
    }

    @OnClick({R.id.tv_copy_order_num, R.id.btn_cancel_order, R.id.bottom_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_copy_order_num:
                String orderNum = tvOrderNum.getText().toString().trim();
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(orderNum);
                toast.show("复制成功");
                break;
            case R.id.btn_cancel_order:
                new HttpRequest<DefaultResponse>()
                        .with(this)
                        .setApiCode("_deleteclassindent_001")
                        .addParam("id",id)
                        .addParam("userToken",MyApp.getInstance().getUserToken())
                        .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                            @Override
                            public void onSuccess(BaseResponse response) {
                                getData();
                            }
                        }).start(new DefaultResponse());
                break;
            case R.id.bottom_btn:

                break;
        }
    }
}
